package cn.itlaobing.web.servlet;



import cn.itlaobing.service.TrainService;
import cn.itlaobing.exception.BusinessException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Description:
 * @Author: WangDong
 * @Date: 2017/11/9 21:50
 */
@WebServlet("/train/trainservlet")
public class TrainServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String trainNum=req.getParameter("trainNum");
        String startStation=req.getParameter("startStation");
        String endStation=req.getParameter("endStation");
        TrainService trainService=new TrainService();
        try {
            Map<String,Object> map=trainService.queryByTrainNum(trainNum,startStation,endStation);
            if(map==null){
                req.setAttribute("MSG","sorry,没有你要查询的车次！");
                req.getRequestDispatcher("/WEB-INF/notrain.jsp").forward(req,resp);
                return;
            }
            System.out.println("/train/trainservlet");
            req.setAttribute("MAP_IN_SESSION",map);
            req.getRequestDispatcher("/WEB-INF/price.jsp").forward(req,resp);
            return;
        } catch (BusinessException e) {
            resp.sendRedirect(req.getContextPath()+"/query.jsp");
            return;
        }
    }
}
